﻿<?php
set_time_limit(0);
//下载fLash游戏,放到当前文件夹
exit();


$url = "http://app.baidu.com/appweb/search?lb=闯关&cid=1&lcid=0&ps=20&pn=1";
$html = file_get_contents($url);

//取得想要的数据放到$out[1]
preg_match("/<div id=\\\"SearchList\\\"(.+)<div id=\\\"PopularRecommend/s",$html,$out);
//到内容中的url部分
preg_match_all('/http:\/\/app.baidu.com\/app\/enter\?appid=(.+?)"/is',$out[1],$arr);
$url_data = array_unique($arr[1]);

foreach ($url_data as $key => $value) {
	a1("http://app.baidu.com/app/enter?appid=".$value);
}

function a1($url){
	$app_html = file_get_contents($url);
	if(empty($app_html))return false;
	
	preg_match('/var appinfo_json \= \{.*?app_name:\\\'(.*?)\\\'.*?flash : \\\'(.*?)\\\'.*?app_logo : \\\'(.*?)\\\'.*?\}/is', $app_html, $out);
	if(count($out) == 4){
		$name = $out[1];
		$swf = $out[2];
		$jpg = $out[3];
		
		downImg($swf, $name.".swf");
		downImg($jpg, $name.".jpg");
		
	}else{
		echo $url."->error\n";
	}
}

function downImg($url = null, $file_name){
	file_put_contents(iconv("UTF-8", "GBK", $file_name),file_get_contents($url));
	return true;
}